<template>
  <div>
    <el-form
        class="detail-form-content"
        ref="ruleForm"
        :rules="rules"
        :model="ruleForm"
        label-width="80px"
    >
      <el-form-item label="原密码" prop="password">
        <el-input v-model="ruleForm.password" show-password></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newpassword">
        <el-input v-model="ruleForm.newpassword" show-password></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="repassword">
        <el-input v-model="ruleForm.repassword" show-password></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onUpdateHandler">确 定</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import {getRole} from "@/utils/getrole";

export default {
  data() {
    return {
      dialogVisible: false,
      ruleForm: {},
      user: {
        password: "",
        adminpassword: "",
        studentpassword: "",
        mentorpassword: "",
      },
      role: '',
      rules: {
        password: [
          {
            required: true,
            message: "密码不能为空",
            trigger: "blur"
          }
        ],
        newpassword: [
          {
            required: true,
            message: "新密码不能为空",
            trigger: "blur"
          }
        ],
        repassword: [
          {
            required: true,
            message: "确认密码不能为空",
            trigger: "blur"
          }
        ]
      }
    };
  },
  mounted() {
    this.getPassword()
  },
  methods: {
    onLogout() {
      this.$storage.remove("Token");
      this.$router.replace({name: "login"});
    },
    getPassword() {
      getRole().then(role => {
        this.role = role
        if (this.role) {
          this.$http({
            url: `${this.role}/token`,
            method: "get",
          }).then(({data}) => {
            if (data && data.code === 0 && this.role === 'admin') {
              this.user.password = data.data.adminpassword
            } else if (data && data.code === 0 && this.role === 'student') {
              this.user.password = data.data.studentpassword
            } else if (data && data.code === 0 && this.role === 'mentor') {
              this.user.password = data.data.mentorpassword
            } else {
              console.error("获取角色失败", data.message);
              throw new Error(data.message || "获取角色失败");
            }
          }).catch(error => {
            this.$message.error(error.message)
          })
        }
      }).catch(error => {
        this.$message.error(error.message)
      })
    },
    // 修改密码
    onUpdateHandler() {
      this.$refs["ruleForm"].validate(valid => {
        if (valid) {

          if (this.ruleForm.newpassword.length < 6){
            this.$message.error("密码不得小于6位")
            return;
          }

          if (this.ruleForm.newpassword != this.ruleForm.repassword) {
            this.$message.error("两次密码输入不一致");
            return;
          }

          this.user.password = this.ruleForm.repassword
          this.user.oldpassword = this.ruleForm.password
          if (this.role === "admin"){
            this.user.adminpassword = this.user.password
          }else if (this.role === 'student'){
            this.user.studentpassword = this.user.password
          }else if (this.role === 'mentor'){
            this.user.mentorpassword = this.user.password
          }
          this.$http({
            url: `${this.role}/update-password`,
            method: "post",
            data: this.user
          }).then(({data}) => {
            if (data && data.data !== false) {
              this.$message({
                message: "修改密码成功,下次登录系统生效，请重新登录",
                type: "success",
                duration: 1500,
                onClose: () => {
                  // 跳转到登录页面
                  this.$router.push("/login");
                }
              });
            } else {
              this.$message.error("请检查原密码");
            }
          });
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
</style>
